package com.example.demo.controller;



import com.example.demo.bean.User;

import com.example.demo.tools.DateTimeTools;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;


@RestController
@RequestMapping("/ai")
public class ChatController {


   private final ChatClient chatClient;



   private ChatMemory chatMemory;

    @Autowired
    public ChatController(ChatClient chatClient, ChatMemory chatMemory) {
        this.chatClient = chatClient;
        this.chatMemory = chatMemory;

    }


    //流式输出
    @RequestMapping(value = "/chat",produces = "text/html; charset=utf-8")
    public Flux<String> chat(@RequestParam(defaultValue = "随机生成一个冷笑话") String prompt, String chatId){

     return   chatClient.prompt()
             .advisors(a-> a.param(ChatMemory.CONVERSATION_ID,chatId))
             .user(prompt)
             .stream()//流式输出
             .content();
    }

    @RequestMapping(value = "/chat2",produces = "text/html; charset=utf-8")
    public String chat2(@RequestParam(defaultValue = "随机生成一个冷笑话")String prompt,String chatId){

        return chatClient.prompt()
                .advisors(a-> a.param(ChatMemory.CONVERSATION_ID,chatId))
                .user(prompt)
                .call()
                .content();
    }

    //结构化输出
    @RequestMapping(value = "/json")
    public Object generationJson(@RequestParam(defaultValue = "随机生成5个电影明星身份信息")String prompt){
        User user = chatClient.prompt()
                .user(prompt)
                .call()
                .entity(new ParameterizedTypeReference<>() {
                });
               // .entity(User.class);

        return ResponseEntity.ok(user);
    }


    //提示词模板：将参数传入prompt中调用
    @GetMapping("/promptTemplate")
    public Object promptTemplate(@RequestParam(defaultValue="三国演义赵子龙")String username){

        return chatClient.prompt()
                .user(u->u.text("请你以{userName}为主题，写一首七言律诗，围绕其生平轶事去写")
                        .param("userName",username))
                .advisors(new SimpleLoggerAdvisor())
                .call()
                .content();
    }



    //上下文记忆
    @GetMapping("chatMemory")
    public Object chatMemory(@RequestParam(defaultValue = "你是哪款大模型") String userInput){
        return chatClient.prompt()
                .user(userInput)
                .advisors(new SimpleLoggerAdvisor(), MessageChatMemoryAdvisor.builder(chatMemory).build())
                .advisors(a->a.param(ChatMemory.CONVERSATION_ID,"testId"))
                .call()
                .content();

    }

    //工具调用
    @GetMapping("/tools")
    public Object tools(String userInput){

        return chatClient.prompt()
                .user(userInput)
                .advisors(new SimpleLoggerAdvisor())
                .tools(new DateTimeTools())
                .call()
                .content();

    }









}
